package com.stu.management.service.impl;


import com.github.pagehelper.PageHelper;
import com.stu.management.mapper.StudentMapper;
import com.stu.management.pojo.Student;
import com.stu.management.pojo.StudentExample;
import com.stu.management.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentDao;

    @Override
    public List<Student> getStudents(String name, String stuno, int sex, int pageIndex, int pageSize) {
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria()
                .andSexEqualTo(sex)
                .andStunameEqualTo(name)
                .andStunoEqualTo(stuno);
        PageHelper.startPage(pageIndex,pageSize);
        List<Student> students = studentDao.selectByExample(studentExample);
        return students;
    }

    @Override
    public int total(String name, String stuno, int sex) {
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria()
                .andSexEqualTo(sex)
                .andStunameEqualTo(name)
                .andStunoEqualTo(stuno);
        return (int) studentDao.countByExample(studentExample);
    }

    @Override
    public int insertStu(Student student) {
        return studentDao.insert(student);
    }

    @Override
    public Student findById(int sid) {
        return studentDao.selectByPrimaryKey(sid);
    }

    @Override
    public int updateStu(Student student) {
        return studentDao.updateByPrimaryKey(student);
    }

    @Override
    public int delStu(String sid) {
        return studentDao.deleteByPrimaryKey(Integer.valueOf(sid));
    }
}
